package com.ynny.bookmanagersystem.controller;

import com.ynny.bookmanagersystem.dao.UserDao;
import com.ynny.bookmanagersystem.dao.UserDaoImpl;
import com.ynny.bookmanagersystem.entity.User;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.List;

@WebServlet("/user/deleteUser")
public class DeleteUserServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request,
                           HttpServletResponse response) throws ServletException, IOException {

        String id = request.getParameter("id");

        UserDao userDao = new UserDaoImpl();

        int i = userDao.deleteUserById(Integer.parseInt(id));

        if (i > 0){
            List<User> users = userDao.selectAllUsers();

            request.getSession().setAttribute("users", users);
            response.sendRedirect(request.getContextPath() + "/user/list.jsp");
        }


    }
}
